
package com.qingsong.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qingsong.dto.PageDTO;
import com.qingsong.dto.RoomInsertDTO;
import com.qingsong.dto.RoomPageDTO;
import com.qingsong.dto.RoomUpdateDTO;
import com.qingsong.entity.Room;
import com.qingsong.excel.RoomExcel;
import com.qingsong.service.RoomService;
import com.qingsong.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingsong
 */
@Tag(name="房间模块")
@RestController
@RequestMapping("/api/v1/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    @Operation(summary = "单增一条记录")
    @PostMapping("save")
    public Object save(@RequestBody @Validated RoomInsertDTO roomInsertDTO){
        return roomService.insert(roomInsertDTO);
    }

    @Operation(summary = "单查一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return roomService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated RoomUpdateDTO roomUpdateDTO){
        return roomService.updateById(roomUpdateDTO);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping ("/remove/{id}")
    public Object remove(@PathVariable Long id){
        return roomService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除多条记录")
    @PostMapping ("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return roomService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping ("list")
    public Object list(){
        return roomService.list();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping ("page")
    public Object page(@RequestBody @Validated PageDTO pageDTO){
        return roomService.page(pageDTO);
    }

    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<RoomExcel> roomExcels = new ArrayList<>();
        List<Room> rooms = roomService.list();
        for (Room room : rooms) {
            RoomExcel roomExcel = BeanUtil.copyProperties(room, RoomExcel.class);
            roomExcels.add(roomExcel);
        }
        EasyExcelUtil.download(resp, "房间统计表.xlsx", "sheet01",
                RoomExcel.class, roomExcels);
    }

    @Operation(summary = "按房间名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated RoomPageDTO dto){
        return roomService.pageLikeTitle(dto);
    }
}
